##################################################
# relative path to parsol sources
PARSOL_PATH=../../Src
# parsol library units that have to be linked into exec, not just headers
PARSOL_SEQ=PS_Common.o PS_CommonStencil.o \
  PS_CommonArray_2D.o PS_CommonArray_3D.o \
  PS_MD5.o
# parsol library parallel units that have to be linked into exec, not just headers
PARSOL_PAR=PS_Topology.o PS_GeneralMPI.o PS_ParallelArray.o


################### COMPILING ########################
# sequential compiling command
SCXX=g++ -pedantic
# parallel compiling command
CXX=mpiCC
# general compiler flags
GENCFL=-I $(PARSOL_PATH)
# flag for selected compiler that will make it to produce dependencies
DEPFL=-MM

# specific compiling flags, may change depending on the task
SPECFL=-O0 -ggdb3 -pg -fprofile-arcs -ftest-coverage -Wall
#SPECFL=-O3

# flags used for compilation
CFLAGS=$(GENCFL) $(SPECFL)

################### LINKING #######################

# sequential linking command
SLNK=g++ -pedantic
# parallel linking command
LNK=mpiCC
# flags used for linking
LFLAGS=$(CFLAGS)

###################################################

EXECS = test_seq multi parmat testmamspeed

exe : $(EXECS)

###################################################

TEST_SEQ=test.o test_metric.o test_mat_struct.o test_matrix.o test_addr_map.o

test_seq : ${TEST_SEQ} $(PARSOL_SEQ)
	$(SLNK) $(LFLAGS) $^ -o $@

include $(TEST_SEQ:.o=.d)
$(TEST_SEQ):
	$(SCXX) $(CFLAGS) -c $< -o $@

###################################################

testmamspeed : testmamspeed.o $(PARSOL_SEQ)
	$(SLNK) $(LFLAGS) $^ -o $@

include testmamspeed.d
testmamspeed.o:
	$(SCXX) $(CFLAGS) -c $< -o $@

###################################################

multi : multi.o $(PARSOL_SEQ)
	$(SLNK) $(LFLAGS) $^ -o $@

include multi.d
multi.o:
	$(SCXX) $(CFLAGS) -c $< -o $@

###################################################

# initial condition and dependency generation rule for local sequential units
%.d: %.cpp
	@echo Calculating dependencies of unit $< ...
	@$(SCXX) $(DEPFL) $(CFLAGS) $< \
	  | sed "s/$*.o/$*.o $*.d/" > $@

###################################################

parmat : parmat.o $(PARSOL_SEQ) $(PARSOL_PAR)
	$(LNK) $(LFLAGS) $^ -o $@

include parmat.dp
parmat.o:
	$(CXX) $(CFLAGS) -c $< -o $@

###################################################

# initial condition and dependency generation rule for local parallel units
%.dp: %.cpp
	@echo Calculating dependencies of parallel unit $< ...
	@$(CXX) $(DEPFL) $(CFLAGS) $< 2> /dev/null \
	  | sed "s/$*.o/$*.o $*.dp/" > $@


##### ParSol specific loading - sequential #########

ifdef PARSOL_SEQ
include $(PARSOL_SEQ:.o=.psd)
$(PARSOL_SEQ):
	$(SCXX) $(CFLAGS) -c $< -o $@
endif

# initial condition and dependency generation rule for ParSol sequential units
%.psd: $(PARSOL_PATH)/%.cpp
	@echo Calculating dependencies of ParSol unit $< ...
	@$(SCXX) $(DEPFL) $(CFLAGS) $< \
	  | sed "s/$*.o/$*.o $*.psd/" > $@


##### ParSol specific loading - parallel #########

ifdef PARSOL_PAR
include $(PARSOL_PAR:.o=.psdp)
$(PARSOL_PAR):
	$(CXX) $(CFLAGS) -c $< -o $@
endif

# initial condition and dependency generation rule for ParSol parallel units
%.psdp: $(PARSOL_PATH)/%.cpp
	@echo Calculating dependencies of ParSol parallel unit $< ...
	@$(CXX) $(DEPFL) $(CFLAGS) $< 2> /dev/null \
	  | sed "s/$*.o/$*.o $*.psdp/" > $@


.PHONY: clean nostat

nostat:
	rm -f *.gcov gmon.out *.da *.gcda

clean: nostat
	rm -f *.o *.d *.dp *.psd *.psdp *.bb *.bbg *.gcno tags $(EXECS)

tags: Makefile *.cpp *.h $(PARSOL_PATH)/*.cpp $(PARSOL_PATH)/*.h
	ctags *.cpp *.h $(PARSOL_PATH)/*.cpp $(PARSOL_PATH)/*.h

# vim: set list:
